package com.xyb.utils;

import com.xyb.config.exception.MyException;

public class FileNameUtils {

    /**
     * 获取文件的名称和最后后缀
     *
     * @param fileName
     * @return
     */
    private static String[] getPrefixAndSuffix(String fileName) {
        String[] strings = new String[2];
        if (fileName == null || fileName.trim().length() == 0) {
            throw new MyException("文件名为空");
        }
        String[] split = fileName.split("\\.");
        if (split.length == 1) {
            strings[0] = fileName;
            strings[1] = "";
            return strings;
        }
        if (split.length == 2) {
            strings[0] = split[0];
            strings[1] = split[1];
            return strings;
        }
        if (split.length > 2) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < split.length - 1; i++) {
                sb.append(split[i])
                        .append(".");
            }
            String prefix = StringUtils.removeTheLastString(sb.toString(), 1);
            strings[0] = prefix;
            strings[1] = split[split.length - 1];
        }
        return strings;
    }

    public static String getPrefix(String fileName){
        String[] fn = getPrefixAndSuffix(fileName);
        return fn[0];
    }

    public static String getSuffix(String fileName){
        String[] fn = getPrefixAndSuffix(fileName);
        return fn[1];
    }

    public static void main(String[] args) {
    }
}
